package com.pzh.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.edu.pojo.Teacher;
import com.pzh.guli.service.edu.pojo.vo.TeacherVo;
import com.pzh.guli.service.edu.service.ITeacherService;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@RestController
@RequestMapping("admin/edu/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    //查询教师所有信息
    @GetMapping("list")
    public Result listAll(){
        List<Teacher> list = teacherService.list();
        return Result.ok().data("items",list);
    }
    //根据id逻辑删除
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable String id){
        teacherService.removeTar(id);
        boolean flag = teacherService.removeById(id);
        if (flag){
            return Result.ok().message("删除成功");
        }
        else {
            return Result.error().message("删除失败");
        }
    }
    //批量删除
    @DeleteMapping("bitch-remove")
    public Result removeByIds(@RequestBody List<String>  ids){
        boolean flag = teacherService.removeByIds(ids);
        if (flag){
            return Result.ok().message("删除成功");
        }
        else {
            return Result.error().message("删除失败");
        }
    }
    //分页查询教师
    @GetMapping("list/{page}/{limit}")
    public Result listPage(@PathVariable Long page, @PathVariable Long limit, TeacherVo teacherVo){
        Page<Teacher> pageParam=new Page(page,limit);
        IPage iPage = teacherService.selectPage(pageParam, teacherVo);
        List<Teacher> records=iPage.getRecords();
        long total = iPage.getTotal();
        return Result.ok().data("total",total).data("rows",records);
    }
    //根据id查询教师
    @GetMapping("get/{id}")
    public Result getById(@PathVariable String id){
        Teacher byId = teacherService.getById(id);
        return Result.ok().data("item",byId);
    }
    //根据id查询教师
    @GetMapping("getNames/{key}")
    public Result getNameList(@PathVariable String key){
        List<Map<String,Object>> list=teacherService.getNameMapList(key);
        return Result.ok().data("item",list);
    }
    //保存信息
    @PutMapping("save")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return Result.ok().message("保存成功");
        }
        else {
            return Result.error().message("保存失败");
        }
    }
    //更新教师信息
    @PutMapping("update")
    public Result updateById(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return Result.ok().message("更新成功");
        }
        else {
            return Result.error().message("更新失败");
        }
    }
}

